<template>
    <div class="qingwu">
        <div class="admin_table_page_title">报警方案</div>
        <div class="unline underm"></div>

        <div class="admin_table_handle_btn">
            <a-form layout="inline" :form="form"  @submit="handleSubmit">
                <a-form-item label="方案名称">
                    <a-input v-model="plan_title"  />
                </a-form-item>

                <a-form-item label="报警类型">
                    <a-select  @change="handleChange" style="width: 120px" v-model="status">
                        <a-select-option  value="" >
                            全部
                        </a-select-option>
                        <a-select-option  value="1" >
                            电费
                        </a-select-option>
                        <a-select-option  value="2" >
                            水费
                        </a-select-option>
                        <a-select-option  value="3" >
                            物业费
                        </a-select-option>
                    </a-select>
                </a-form-item>

                <a-form-item label="创建时间">

                    <a-range-picker v-model="change" @change="onChanged" />
                </a-form-item>

                <a-form-model-item >
                    <a-button icon="search" @click="search">
                        查询
                    </a-button>
                </a-form-model-item>
                <a-form-model-item >
                    <a-button  @click="reset">重置</a-button>
                </a-form-model-item>
            </a-form>
        </div>

        <div class="admin_table_handle_btn">
            <a-button @click="$router.push('/Estate/AlarmPlan/form')" type="primary" icon="plus">添加</a-button>
            <a-button class="admin_delete_btn" type="danger" icon="delete" @click="dels">批量删除</a-button>
        </div>
        <div class="admin_table_list">
            <a-table :columns="columns" :data-source="list" :pagination="false" :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" row-key="id">



                <div slot="balancedata" slot-scope="rows">

                        <span v-if="rows.alarm_type == 1" class="layui-badge layui-bg-blue">电费</span>
                        <span v-else-if="rows.alarm_type == 2" class="layui-badge layui-bg-blue">水费</span>
                        <span v-else-if="rows.alarm_type == 3" class="layui-badge layui-bg-orange">物业</span>

                </div>
                <div slot="remarks" slot-scope="rows">
                    <div v-html="rows.remarks"></div>
                </div>


                <span slot="status" slot-scope="rows">
                    <a-switch v-if="rows.status == 1" checked-children="正常" un-checked-children="禁用" @change="onChangeStatus('status', rows.id)" default-checked/>
                    <a-switch v-else checked-children="正常" @change="onChangeStatus('status', rows.id)" un-checked-children="禁用" />
                </span>

                <span slot="action" slot-scope="rows">
                    <a-button size="small" v-if="rows.is_default == 0" @click="onChangeStatus('is_default', rows.id)">设为默认</a-button>
                    <a-button size="small" icon="edit" @click="$router.push('/Estate/AlarmPlan/form/'+rows.id)">编 辑</a-button>
                    <a-button size="small" icon="edit" @click="onChangeStatus('is_deleted', rows.id)">删 除</a-button>
                </span>
            </a-table>
            <div class="admin_pagination" v-if="total>0">
                <a-pagination v-model="params.page" :page-size.sync="params.per_page" :total="total" @change="onChange" show-less-items />
            </div>
        </div>
    </div>
</template>

<script>
export default {
    components: {},
    props: {},
    data() {
      return {
          params:{
              page:1,
              per_page:30,
          },
          total:0, //总页数
          selectedRowKeys:[], // 被选择的行
          columns:[
              {title:'#',dataIndex:'id',fixed:'left'},
              {title:'方案名称',dataIndex:'plan_title'},
              {title:'提前报警余额',dataIndex:'early_warning_balance'},
              {title:'报警余额',dataIndex:'alarm_balance'},
              {title:'报警时间间隔',dataIndex:'alarm_time'},
              {title:'描述',scopedSlots: { customRender: 'remarks' }},
              {title:'方案类型',scopedSlots: { customRender: 'balancedata' }},
              {title: '状态', scopedSlots: { customRender: 'status' }},
              {title:'添加时间',dataIndex:'create_time'},
              {title:'操作',key:'id',fixed:'right',scopedSlots: { customRender: 'action' }},
          ],
          list:[],
          status:'',
          change:'',
      };
    },
    watch: {},
    computed: {},
    methods: {
        // 选择框被点击
        onSelectChange(selectedRowKeys) {
            this.selectedRowKeys = selectedRowKeys;
        },
        // 选择分页
        onChange(e){
            this.params.page = e;
            this.onload();
        },
        onChanged(date, dateString) {
            this.change = dateString
            // console.log(date, dateString);
        },
        // 状态
        handleChange(value, selectedOptions){
            this.status = value;
        },
        // 修改状态
        onChangeStatus(type,id){
            this.$post(this.$api.editAlarmPlanStatus,{type:type,id:id}).then(res=>{
                if(res.code == 200){
                    this.$message.success(res.msg)

                }else{
                    return this.$message.error(res.msg)
                }

                this.onload();
            })
        },
        // 删除
        dels(){
            if(this.selectedRowKeys.length==0){
                return this.$message.error('未选择数据.');
            }
            this.$confirm({
                title: '你确定要删除选择的数据？',
                content: '确定删除后无法恢复.',
                okText: '是',
                okType: 'danger',
                cancelText: '取消',
                onOk:()=> {
                    let ids = this.selectedRowKeys.join(',');
                    this.$delete(this.$api.editAlarmPlanDel+'/'+ids).then(res=>{
                        if(res.code == 200){
                            this.onload();
                            this.$message.success('删除成功');
                        }else{
                            this.$message.error(res.msg)
                        }
                    });

                },
            });
        },
        onload(){
            this.$get(this.$api.estateAlarmPlan,this.params).then(res=>{
                this.total = res.data.total;
                this.list = res.data.data;
            });
        },
        // 重置
        reset(){
            location.reload();
        },
        search(){
            this.params['plan_title'] = this.plan_title;
            this.params['change'] = this.change;
            this.params['status'] = this.status;
            this.$get(this.$api.estateAlarmPlan,this.params).then(res=>{
                this.total = res.data.total;
                this.list = res.data.data;
            });
        },
    },
    created() {
        this.onload();
    },
    mounted() {}
};
</script>
<style lang="scss" scoped>

</style>
